﻿using AutoMapper;
using Microsoft.AspNetCore.Mvc;
using SmartHotel.Dto;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.ObjectMapping;

namespace SmartHotel.Menu
{
    [ApiExplorerSettings(GroupName = "v2")]
    #region

    //public class SmartHotelAppMenuService :
    //    CrudAppService<
    //        MenuTable,
    //        MenuDto,
    //        int,
    //        PageMenuResultDto, //Used for paging/sorting
    //        CreateandUpdateMenu>, ISmartHotelAppMenuService//Used to create/update a book

    //{
    //    public SmartHotelAppMenuService(IRepository<MenuTable, int> repository) : base(repository)
    //    {

    //    }
    //}
    #endregion
    public class SmartHotelAppMenuService :ApplicationService, ISmartHotelAppMenuService
    {
        private readonly IRepository<MenuTable, int> _todoItemRepository;
        public SmartHotelAppMenuService(IRepository<MenuTable, int> _todoItemRepository)
        { 
            this._todoItemRepository = _todoItemRepository;
        }
        /// <summary>
        /// 添加菜单权限
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task<ResultDto<CreateandUpdateMenu>> CreateMenu(CreateandUpdateMenu model)
        {
            var list = await _todoItemRepository.InsertAsync(ObjectMapper.Map<CreateandUpdateMenu,MenuTable>(model));
            return new ResultDto<CreateandUpdateMenu> { Code = Result.Success, Data = list };
        }
        /// <summary>
        /// 反填菜单
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<ResultDto<MenuDto>> GetByIdMenu(int id)
        {
            var list=await _todoItemRepository.FindAsync(id);
            return new ResultDto<MenuDto> { Code = Result.Success, Data = list };
        }
        /// <summary>
        /// 递归菜单显示
        /// </summary>
        /// <returns></returns>
        public async Task<ResultDto<List<MenuTreeDto>>> GetListMenu()
        {
            var list = await _todoItemRepository.GetQueryableAsync();
            return new ResultDto<List<MenuTreeDto>> { Code = Result.Success,Data= GetTrees(list.ToList(),0) };
        }

        private List<MenuTreeDto> GetTrees(List<MenuTable> menus, int ParentId)
        { 
            List<MenuTreeDto> tree=new List<MenuTreeDto>();
            var list = menus.Where(x => x.ParentId == ParentId);
            foreach (var menu in list) 
            {
                var treeDto = ObjectMapper.Map<MenuTable, MenuTreeDto>(menu);
                treeDto.children = !GetTrees(menus, menu.Id).Any() ? null : GetTrees(menus, menu.Id);
                tree.Add(treeDto);
            }
            return tree;
        }
        /// <summary>
        /// 菜单删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<ResultDto<MenuDto>> DeleteMenu(int id)
        {
            await _todoItemRepository.DeleteAsync(id);
            return new ResultDto<MenuDto> { Code = Result.Success };

        }
        /// <summary>
        /// 菜单的修改
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task<ResultDto<CreateandUpdateMenu>> UpdateMenu(CreateandUpdateMenu model)
        {
            var menu = await _todoItemRepository.GetAsync(model.Id);
            // await _todoItemRepository.UpdateAsync(b);
            menu = ObjectMapper.Map(model, menu);
            await _todoItemRepository.UpdateAsync(menu);
            var list = ObjectMapper.Map<MenuTable, CreateandUpdateMenu>(menu);

            return new ResultDto<CreateandUpdateMenu> { Code = Result.Success, Data = list };
        }
        /// <summary>
        /// 菜单的分页
        /// </summary>
        /// <returns></returns>
        public async Task<ResultDto<List<MenuDto>>> GetPageMenuList()
        {
            var list=await _todoItemRepository.GetListAsync();
            return new ResultDto<List<MenuDto>> {Code=Result.Success,Data=list };
        }
    }

}
